<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>创建一个新的代码对象</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="class.mongocode.html">MongoCode</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongocode.tostring.html">MongoCode::__toString</a></div>
 <div class="up"><a href="class.mongocode.html">MongoCode</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mongocode.construct" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">MongoCode::__construct</h1>
  <p class="verinfo">(PECL mongo &gt;= 0.8.3)</p><p class="refpurpose"><span class="refname">MongoCode::__construct</span> &mdash; <span class="dc-title">创建一个新的代码对象</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-mongocode.construct-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>MongoCode::__construct</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$code</code></span>
   [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$scope</code><span class="initializer"> = array()</span></span>
  ] )</div>

 </div>


 <div class="refsect1 parameters" id="refsect1-mongocode.construct-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>

      <em><code class="parameter">code</code></em>
     </dt>

     <dd>

      <p class="para">
       字符串代码。
      </p>
     </dd>

       
    
     <dt>

      <em><code class="parameter">scope</code></em>
     </dt>

     <dd>

      <p class="para">
       使用代码的范围。
      </p>
     </dd>

       
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-mongocode.construct-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回一个新的代码对象。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-mongocode.construct-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-1462">
   <p><strong>Example #1 <span class="methodname"><strong>MongoCode::__construct()</strong></span> 例子</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$code&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoCode</span><span style="color: #007700">(</span><span style="color: #DD0000">'function()&nbsp;{&nbsp;'</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'for(i=0;i&lt;10;i++)&nbsp;{'</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'db.foo.update({z&nbsp;:&nbsp;i},&nbsp;{z&nbsp;:&nbsp;x});'</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'}'</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'return&nbsp;x-1;'</span><span style="color: #007700">.<br />&nbsp;</span><span style="color: #DD0000">'}'</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #DD0000">"x"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$code</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
object(MongoCode)#1 (2) {
  [&quot;scope&quot;]=&gt;
  array(1) {
    [&quot;x&quot;]=&gt;
    int(4)
  }
  [&quot;code&quot;]=&gt;
  string(80) &quot;function() { for(i=0;i&lt;10;i++) { db.foo.update({z : i}, {z : x}); } return x-1; }&quot;
}
</pre></div>
   </div>
  </div>


  <div class="example" id="example-1463">
   <p><strong>Example #2 使用具有 $where 的 <a href="class.mongocode.html" class="classname">MongoCode</a></strong></p>
   <div class="example-contents"><p>
    这个例子查询了集合里，&#039;x&#039; 字段比 $y 小的元素。
    注意，PHP 对象能够传入到 JavaScript 作用域，然后 JavaScript 函数返回一个 boolean。
   </p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$cursor&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$collection</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">find</span><span style="color: #007700">(array(</span><span style="color: #DD0000">'$where'&nbsp;</span><span style="color: #007700">=&gt;&nbsp;new&nbsp;</span><span style="color: #0000BB">MongoCode</span><span style="color: #007700">(</span><span style="color: #DD0000">'function()&nbsp;{&nbsp;return&nbsp;this.x&nbsp;&lt;&nbsp;y;&nbsp;}'</span><span style="color: #007700">,&nbsp;array(</span><span style="color: #DD0000">'y'</span><span style="color: #007700">=&gt;</span><span style="color: #0000BB">$y</span><span style="color: #007700">))));<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="class.mongocode.html">MongoCode</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongocode.tostring.html">MongoCode::__toString</a></div>
 <div class="up"><a href="class.mongocode.html">MongoCode</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
